package org.space.ai.controller;

import com.galaxy.common.core.domain.R;
import com.galaxy.common.redis.utils.RedisUtils;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.space.ai.service.MoonshotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class ImageAIController {

    @Autowired
    private final MoonshotService moonshotService;

    @PostMapping("/chat")
    public ResponseEntity<String> chat(@RequestBody Map<String, String> body) {
        String prompt = body.get("prompt");
        String response = moonshotService.getResponse(prompt);
        return ResponseEntity.ok(response);
    }
    @PostMapping("/generate-caption")
    public String generateCaption(@RequestParam("image") MultipartFile imageFile) {
        return moonshotService.generateCaption(imageFile);
    }
    @PostMapping("/feedbackContentVerification")
    public R<String> feedbackContentVerification(@RequestParam("image") MultipartFile imageFile, String content, String contentType,Long userId) {
        Object feedback = RedisUtils.getCacheObject("feedback:"+userId);
        if(feedback != null){
            int i = Integer.parseInt(feedback.toString());
            if(i > 2){
                return R.ok("你提交的错误信息过多，请明天在尝试","false");
            }
        }

        return moonshotService.feedbackContentVerification(imageFile, content, contentType,userId);
    }


    @PostMapping("/feedbackContentVerification1")
    public R<String> feedbackContentVerification(@RequestBody Feedback feedback1) {
        Object feedback = RedisUtils.getCacheObject("feedback:"+feedback1.getUserId());
        if(feedback != null){
            int i = Integer.parseInt(feedback.toString());
            if(i > 2){
                return R.ok("你提交的错误信息过多，请明天在尝试","false");
            }
        }

        return moonshotService.feedbackContentVerification(feedback1.getContent(), feedback1.getContentType(),feedback1.getUserId());
    }

    @Data
    public static class Feedback {
        private String content;
        private String contentType;
        private Long userId;
    }
}
